package com.resys.server.controller.staff;

import com.resys.entity.dto.LogDto;
import com.resys.entity.dto.TokenUserInfoDto;
import com.resys.entity.enums.ResponseCodeEnum;
import com.resys.entity.po.Cancellationrequest;
import com.resys.entity.po.Meetingroom;
import com.resys.entity.po.Reservationorder;
import com.resys.entity.query.CancellationrequestQuery;
import com.resys.entity.query.ReservationorderQuery;
import com.resys.entity.vo.ResponseVO;
import com.resys.exception.BusinessException;
import com.resys.server.controller.ABaseController;
import com.resys.service.CancellationrequestService;
import com.resys.service.MeetingroomService;
import com.resys.service.ReservationorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RequestMapping("/staff/passOrders")
@Api(tags = "员工管理-审核订单")
@RestController("passOrdersController")
public class PassOredrsController extends ABaseController {
    @Resource
    private CancellationrequestService cancellationrequestService;
    @Resource
    private ReservationorderService reservationorderService;
    @Resource
    private MeetingroomService meetingroomService;
    @ApiOperation("展示退款取消申请列表")
    @GetMapping("/listrequest")
    public ResponseVO listrequest() {
         CancellationrequestQuery query = new CancellationrequestQuery();
         query.setStatus(0);
        List<Cancellationrequest> list = cancellationrequestService.findListByParam(query);
        return getSuccessResponseVO(list);
    }
    @ApiOperation("接受退款")
    @PostMapping("/pass")
    public ResponseVO pass(Integer cancellationId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo("staff");
        if(tokenUserInfoDto==null){
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        Cancellationrequest cancell = cancellationrequestService.getCancellationrequestByCancellationId(cancellationId);
        if(cancell==null){
            return getSuccessResponseVO("申请不存在");
        }
        cancell.setStatus(1);
        cancell.setProcessedBy(tokenUserInfoDto.getUserId());
        cancell.setProcessedTime(new Date());
        cancellationrequestService.updateCancellationrequestByCancellationId(cancell,cancellationId);
        Reservationorder od = reservationorderService.getReservationorderByOrderId(cancell.getOrderId());
        od.setStatus(2);
        od.setEndTime(new Date());
        reservationorderService.updateReservationorderByOrderId(od,od.getOrderId());
        Meetingroom room = meetingroomService.getMeetingroomByRoomId(od.getRoomId());
        if(room==null){
            return getSuccessResponseVO("会议室已不存在，退款成功");
        }
        room.setStatus(0);
        LogDto logDto = new LogDto();
        logDto.setOldStatus(2);
        logDto.setNewStatus(0);
        logDto.setRoomId(room.getRoomId());
        logDto.setUserId(tokenUserInfoDto.getUserId());
        writelog(logDto);
        meetingroomService.updateMeetingroomByRoomId(room,od.getRoomId());
        return getSuccessResponseVO("处理成功");
    }
    @ApiOperation("拒绝退单")
    @PostMapping("/refuse")
    public ResponseVO refuse(Integer cancellationId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfo("staff");
        if(tokenUserInfoDto==null){
            throw new BusinessException(ResponseCodeEnum.CODE_901);
        }
        Cancellationrequest cancell = cancellationrequestService.getCancellationrequestByCancellationId(cancellationId);
        cancell.setStatus(2);
        cancell.setProcessedBy(tokenUserInfoDto.getUserId());
        cancell.setProcessedTime(new Date());
        cancellationrequestService.updateCancellationrequestByCancellationId(cancell,cancellationId);
        return getSuccessResponseVO("处理成功");
    }
}
